package org.zfes.snowier.cloud.zbss.layer.tc.biz.controller;

import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.auth.SessionUser;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.consts.BizConst;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.LAuthUser;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.ILAuthUserService;
@Controller 
@RequestMapping("/tcms/lauthUser")
public class LAuthUserController extends BaseController {

	private String viewPathPrefix="/tcms";
	
	@Autowired
	private ILAuthUserService authUserService;
	
	@RequestMapping(value={"/",""}, method = RequestMethod.GET,produces = MediaType.TEXT_HTML_VALUE)
    public Object index(){
		return forward(viewPathPrefix,"authUser.html");
    }
	
    @RequestMapping(value="/loadLAuthUserDataSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object loadLAuthUserDataSet(@RequestParam Map<String,Object> params){
    	params.put("role", BizConst.ROLE.R_ADMIN);
	    DataSet dataSet=  authUserService.loadAuthUserDataSet(params);
	    return dataSet;
    }
	
    @RequestMapping(value="/addLAuthUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object addLAuthUser(LAuthUser lauthuser){
		authUserService.createAuthUser(lauthuser);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    
    @RequestMapping(value="/changePwd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object changePwd(HttpServletRequest httpRequest,String password,String  password2){
    	ZAssert.hasText(password, "密码不能为空");
    	ZAssert.hasText(password2, "确认密码不能为空");
    	ZAssert.equals(password, password2, "密码输入不一致");
    	SessionUser su= (SessionUser) httpRequest.getSession().getAttribute(BizConst.SESSION_USER);
    	if(su==null) {
    		ZAlert.serviceLogicalError("登录超时");
    	}
    	
		authUserService.setPwd(su.getUserId(),password);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    
    @RequestMapping(value="/setPwd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object setPwd(Long id,String  password){
		authUserService.setPwd(id,password);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    
	
    @RequestMapping(value="/loadLAuthUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object loadLAuthUser(Long id){
		Optional<LAuthUser> ops = authUserService.loadAuthUserById(id);
		return ajaxQuery("authUser",ops);
    }
	
    @RequestMapping(value="/updateLAuthUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object updateLAuthUser(LAuthUser authUser){
		authUserService.updateAuthUser(authUser);
		return ajaxDoneSuccess("数据操作成功 ");
    }
	
    @RequestMapping(value="/deletLAuthUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object deletLAuthUser(Long id){
		authUserService.deletAuthUser(id);
		return ajaxDoneSuccess("数据操作成功 ");
    }

}